#include <sys/epoll.h>
#include <cstring>
#include "nocopy.hpp"
#include "Log.hpp"
#include "Socket.hpp"

const static int epfd_size=64;
class Epoller: public nocopy
{
public:
    Epoller()
    {
        _epfd=epoll_create(epfd_size);
        if (_epfd == -1)
        {
            lg(Error, "epoll_create error: %s", strerror(errno));
        }
        else
        {
            lg(Info, "epoll_create success: %d", _epfd);
        }
    }
     int EpollerWait(struct epoll_event revents[], int num)
     {
        int n=epoll_wait(_epfd,revents,num,-1);
        return n;
     }
     int EpllerUpdate(int oper, int sock, uint32_t event)
     {
        int n=0;
        if (oper == EPOLL_CTL_DEL)
        {
            n = epoll_ctl(_epfd, EPOLL_CTL_DEL, sock, nullptr);
            if (n < 0)
            {
                lg(Error, "Epoll del fail", strerror(errno));
            }
        }
        else
        {
            struct epoll_event ev;
            ev.events = event;
            ev.data.fd = sock; // 目前，方便我们后期得知，是哪一个fd就绪了！
            n = epoll_ctl(_epfd, oper, sock, &ev);
            if (n != 0)
            {
                lg(Error, "epoll_ctl error!");
            }
        }
        return n;
     }
     ~Epoller()
    {
        if (_epfd >= 0)
            close(_epfd);
    }

private:
    int _epfd;
    int _timeout{3000};
};